<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2017/8/21
 * Time: 20:06
 */

namespace app\admin\controller\order;

use app\admin\model\Order as OrderModel;
use app\common\controller\Backend;
use app\common\model\Category as CategoryModel;
use think\Session;

/**
 * Class Order
 * @package app\admin\controller\staff
 * 此类为我的面试控制器
 */
class Myinterview extends Backend
{
	protected $model = null;
	
	public function _initialize()
	{
		parent::_initialize();
		$this->model = model('Worklog');
		$facetypelist = db('facetype')->select();
		$serverdaytpyelist=db('serverdays')->select();
		foreach ($serverdaytpyelist as $key=>$value){
			$daydata[$value['id']]=$value['server_days'];
		}
		$this->view->assign('daylist',$daydata);
		$this->view->assign('facetypelist', $facetypelist);
	}
	
	/**
	 * 此函数为显示为员工的面试列表
	 */
	public function index()
	{
		$order_id=$_GET['orderid'];
		$this->view->assign('orderid',$order_id);
		
		if ($this->request->isAjax()) {
			$order=db('order')->where('id',$order_id)->find();
			list($where, $sort, $order, $offset, $limit) = $this->buildparams(NULL);
			$total = $this->model->with('order')->where('w_type',3)
				->where($where)
				->order($sort, $order)
				->count();
			$list = $this->model->with('order')->where('w_type',3)
				->where($where)
				->order($sort, $order)
				->limit($offset, $limit)
				->select();
			$result = array("total" => $total, "rows" => $list);
			return json($result);
		}
		return $this->view->fetch();
	}
	
	/**
	 * @param null $ids
	 * @return string|void
	 * 编辑面试
	 */
	public function edit($ids = NULL)
	{
		$row = $this->model->get($ids);
		if (!$row)
			$this->error(__('No Results were found'));
		if ($this->request->isPost()) {
			$_POST['row']['endtime'] = date("Y-m-d", strtotime("+1 day", strtotime($_POST['row']['begintime'])));
			$this->code = -1;
			$isfree=db('worklog')
				->where('begintime','<= time',$_POST['row']['begintime'])
				->where('endtime','> time',$_POST['row']['begintime'])
				->whereor('begintime',['>=',$_POST['row']['begintime']],['<',$_POST['row']['endtime']],'and')
				->whereor('endtime',['>',$_POST['row']['begintime']],['<',$_POST['row']['endtime']],'and')
				->find();
			if($isfree!=null){
				$this->code = -1;
				$this->msg="这一天已有安排，请查看！";
				return;
			}
			
			try {
				//是否采用模型验证
				if ($this->modelValidate) {
					$name = basename(str_replace('\\', '/', get_class($this->model)));
					$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
					$row->validate($validate);
				}
				$result = $row->save($_POST['row']);
				if ($result !== false) {
					$this->code = 1;
				} else {
					$this->msg = $row->getError();
				}
			} catch (think\exception\PDOException $e) {
				$this->msg = $e->getMessage();
			}
			
			
			return;
		}
		$this->view->assign("row", $row);
		return $this->view->fetch();
	}
	
	/**
	 * 面试通过后可增加工作
	 */
	public function  addworklog(){
		/***
		 * step1:c添加工作安排
		 */
	
		if ($this->request->isPost()) {
			$this->code = -1;
			$resultdata=db('worklog')->where('order_id',$_POST['row']['order_id'])->where('w_type',2)->find();
			if($resultdata){
				$this->msg ="工作已安排";
				return;
			}
			$resultdata=db('worklog')->where('order_id',$_POST['row']['order_id'])->where('w_type',4)->find();
			$data['begintime']=$_POST['row']['begintime'];
			$data['endtime']=date("Y-m-d",strtotime("+".$_POST['row']['serverdays']." day",strtotime($_POST['row']['begintime'])));
			$data['order_id']=$_POST['row']['order_id'];
			$data['serverdays']=$_POST['row']['serverdays'];
			$data['staff_id']=$_POST['row']['staff_id'];
			$data['admin_id']=Session::get('admin')->id;
			$data['createtime']=date('Y-m-d',time());
			$data['w_type']=2;
			$user = model('Worklog');
			$order = OrderModel::get(intval($_POST['row']['order_id']));
			$order->status     = 1;
			$order->save();
			$result=$user->where('id',$resultdata['id'])->update($data);
			if ($result !== false)
			{
				$this->code = 1;
			}
			else
			{
				$this->msg = $this->model->getError();
			}
			return;
		}
		else{
			$id=$_GET['id'];
			$row=db('worklog')->where('id',$id)->find();
			$data=db('order')->where('id',$row['order_id'])->find();
			$this->view->assign('orderdata',$data);
			$this->view->assign('row',$row);
			return $this->view->fetch();
		}
	}
	
}